<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/2
 * Time: 上午9:14
 */
namespace Common\Model;
use Think\Model;
class GuestbookModel extends Model
{
    protected $table;//无表名前缀的管理员表名
    protected $guestTable;//有前缀的管理员表
    protected $prefix;//表前缀

    /**构造函数初始化数据
     * @param string $adminTable
     * @param string $prefix
     */
    public function __construct($guestTable, $prefix = '')
    {
        parent::__construct();
        $this->prefix = empty($prefix) ? C('DB_PREFIX') : $prefix;
        $this->table = $guestTable;
        $this->menuTable = $this->prefix . $guestTable;
    }

    /**此方法用于返回值
     * @param $data
     * @param string $msg
     * @param int $status
     * @return array
     */
    public function returnInfo($data,$msg="",$status=1){
        return (['status'=>$status,'msg'=>$msg,'data'=>$data]);
    }

    /**此方法用于从数据表读取记录。读取一条记录用于编辑，读取全部记录则用于展示。
     * @param $id
     * @param string $field
     * @param string $order
     * @return array
     *
     */
    public function guestList($field = 'id',$order ='id desc'){
        $list = [];
        $guest = M($this -> table);
        $list = $guest->field($field);
        $list = $list->order($order)->select();
        if(!$list){
            return $this -> returnInfo('','读取数据失败！',1);
        }
        return $this -> returnInfo($list,'读取数据成功！',0);
    }


    /**此方法用于修改审核状态
     * @param $id
     * @return array
     */
    public function changeStatus($id){
        $guest = M($this -> table);
        $guestinfo = $guest->field('status')->where(['id'=>$id])->find();
        if($guestinfo['status'] == 0){
            $status = $guest ->where(['id'=>$id])-> setField(['status'=>1]);
            if(!$status){
                $result = $this -> returnInfo(0,'修改失败！',1);
            }
            $result = $this -> returnInfo(1,'已审核！',0);
        }else{
            $status = $guest->where(['id'=>$id])->setField(['status'=>0]);
            if(!$status){
                $result = $this -> returnInfo(0,'修改失败！',1);
            }
            $result = $this -> returnInfo(0,'未审核！',0);
        }
        return $result;
    }
  /*  public function changeStatus($id){
        $guest = M($this -> table);
        $guestinfo = $guest->field('status')->where(['id'=>$id])->find();
        if($guestinfo['status'] == 0){
            $status = $guest ->where(['id'=>$id])-> setField(['status'=>1]);
            if(!$status){
                $result = $this -> returnInfo(0,'修改失败！',1);
            }
            $result = $this -> returnInfo(1,'已审核！',0);
        }else{
            $status = $guest->where(['id'=>$id])->setField(['status'=>0]);
            if(!$status){
                $result = $this -> returnInfo(0,'修改失败！',1);
            }
            $result = $this -> returnInfo(0,'未审核！',0);
        }
        return $result;
    }*/
    public function delguest($id){
        $del = M('guestbook')->where(['id'=>$id])->delete();
        if(!$del){
            return $this -> returnInfo(0,'删除失败！',1);
        }
        return $this -> returnInfo(1,'删除成功！',0);
    }
}